latticeパッケージを使ってカラーキー付の図を描く。latticeExtraパッケージを使ってレイヤーとして図を重ね書きする。
library(lattice)
library(latticeExtra)
グリッド形式のデータを扱う
x <- seq(1,10,by=1)
y <- seq(1,10,by=1)
d <- expand.grid(x=x,y=y)
d$z <- d$x*d$y
levelplot関数でカラーキー付の塗り分け図を描く.
levelplot(z~x*y, data=d, asp=1)
色パレットの指定はcol.regionsで指定する.
levelplot(z~x*y, data=d, asp=1, col.regions=terrain.colors)
色の塗り分けの区分を指定したい場合はatに(最小値,区分1,…,区分K,最大値)を指定する
brks <- c(min(d$z),20,40,60,80,max(d$z))
levelplot(z~x*y, data=d, asp=1, ,col.regions=terrain.colors, at=brks)
contourplot関数で等高線図を描く.
contourplot(z~x*y, data=d, asp=1)
複数の図を重ねる場合はlatticeExtraパッケージのas.layer関数を用いると簡単.
fig <- levelplot(z~x*y, data=d, asp=1, col.regions=terrain.colors, at=brks)
fig + as.layer(contourplot(z~x*y, data=d, asp=1, at=brks))
as.layer関数はテキスト図には非対応なので、テキスト図を重ね書きする時はlayer関数を使う.
fig <- levelplot(z~x*y, data=d, asp=1, col.regions=terrain.colors)
fig + layer(data=d,panel.text(x,y,z))